/*     */ package com.zimbra.cs.service.account;
/*     */ 
/*     */ import com.zimbra.common.account.Key.DistributionListBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AccountConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.Group;
/*     */ import com.zimbra.cs.account.Group.GroupOwner;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.gal.GalGroupMembers;
/*     */ import com.zimbra.cs.gal.GalGroupMembers.DLMembers;
/*     */ import com.zimbra.cs.gal.GalGroupMembers.DLMembersResult;
/*     */ import com.zimbra.cs.gal.GalGroupMembers.LdapDLMembers;
/*     */ import com.zimbra.cs.gal.GalGroupMembers.ProxiedDLMembers;
/*     */ import com.zimbra.cs.gal.GalSearchControl;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GetDistributionListMembers
/*     */   extends GalDocumentHandler
/*     */ {
/*     */   private static final String A_PROXIED_TO_HOME_OF_GROUP = "__proxied__";
/*     */   
/*     */   protected Element proxyIfNecessary(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  52 */     boolean proxiedToHomeOfInternalGroup = request.getAttributeBool("__proxied__", false);
/*     */     
/*  54 */     if (proxiedToHomeOfInternalGroup)
/*     */     {
/*  56 */       return null;
/*     */     }
/*     */     
/*  59 */     return super.proxyIfNecessary(request, context);
/*     */   }
/*     */   
/*     */ 
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  66 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  67 */     Account account = getRequestedAccount(zsc);
/*     */     
/*  69 */     if (!canAccessAccount(zsc, account)) {
/*  70 */       throw ServiceException.PERM_DENIED("can not access account");
/*     */     }
/*     */     
/*  73 */     Element eDL = request.getElement("dl");
/*  74 */     String dlName = eDL.getText();
/*     */     
/*  76 */     int offset = getOffset(request);
/*  77 */     int limit = getLimit(request);
/*     */     
/*     */ 
/*  80 */     request.addAttribute("offset", (String)null);
/*  81 */     request.addAttribute("limit", (String)null);
/*  82 */     request.addAttribute("_offset", offset);
/*  83 */     request.addAttribute("_limit", limit);
/*     */     
/*  85 */     boolean proxiedToHomeOfInternalGroup = request.getAttributeBool("__proxied__", false);
/*     */     
/*  87 */     GalGroupMembers.DLMembersResult dlMembersResult = null;
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*  92 */     Provisioning prov = Provisioning.getInstance();
/*  93 */     Group group = prov.getGroupBasic(Key.DistributionListBy.name, dlName);
/*  94 */     if (group != null)
/*     */     {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 103 */       dlMembersResult = getMembersFromLdap(context, request, account, group, proxiedToHomeOfInternalGroup);
/*     */     }
/*     */     
/* 106 */     if (dlMembersResult == null)
/*     */     {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 131 */       boolean hideInGal = (group != null) && (group.hideInGal());
/* 132 */       if ((!hideInGal) && (!proxiedToHomeOfInternalGroup)) {
/* 133 */         dlMembersResult = GalGroupMembers.searchGal(zsc, account, dlName, request);
/*     */       }
/*     */     }
/*     */     
/* 137 */     if (dlMembersResult == null) {
/* 138 */       throw AccountServiceException.NO_SUCH_DISTRIBUTION_LIST(dlName);
/*     */     }
/*     */     
/* 141 */     if ((dlMembersResult instanceof GalGroupMembers.ProxiedDLMembers))
/* 142 */       return ((GalGroupMembers.ProxiedDLMembers)dlMembersResult).getResponse();
/* 143 */     if ((dlMembersResult instanceof GalGroupMembers.DLMembers)) {
/* 144 */       return processDLMembers(zsc, dlName, account, limit, offset, (GalGroupMembers.DLMembers)dlMembersResult);
/*     */     }
/* 146 */     throw ServiceException.FAILURE("unsopported DLMembersResult class: " + dlMembersResult.getClass().getCanonicalName(), null);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private GalGroupMembers.DLMembersResult getMembersFromLdap(Map<String, Object> context, Element request, Account account, Group group, boolean proxiedToHomeOfInternalGroup)
/*     */     throws ServiceException
/*     */   {
/* 171 */     boolean local = (proxiedToHomeOfInternalGroup) || (Provisioning.onLocalServer(group));
/* 172 */     if (local)
/*     */     {
/* 174 */       return getMembersFromLdap(account, group);
/*     */     }
/* 176 */     Server server = group.getServer();
/* 177 */     if (server == null)
/*     */     {
/* 179 */       ZimbraLog.account.warn("unable to find home server (%s) for group %s, getting members from LDAP on local server", new Object[] { group.getAttr("zimbraMailHost"), group.getName() });
/*     */       
/*     */ 
/*     */ 
/* 183 */       return getMembersFromLdap(account, group);
/*     */     }
/*     */     
/* 186 */     ZimbraLog.account.debug("Proxying request to home server (%s) of group %s", new Object[] { server.getName(), group.getName() });
/*     */     
/*     */ 
/* 189 */     request.addAttribute("__proxied__", true);
/*     */     try {
/* 191 */       Element resp = proxyRequest(request, context, server);
/* 192 */       return new GalGroupMembers.ProxiedDLMembers(resp);
/*     */     }
/*     */     catch (ServiceException e) {
/* 195 */       request.addAttribute("__proxied__", (String)null);
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 200 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private GalGroupMembers.DLMembersResult getMembersFromLdap(Account account, Group group)
/*     */     throws ServiceException
/*     */   {
/* 208 */     boolean allow = false;
/* 209 */     if (group.hideInGal()) {
/* 210 */       allow = Group.GroupOwner.isOwner(account, group);
/*     */     } else {
/* 212 */       allow = (Group.GroupOwner.isOwner(account, group)) || (group.isMemberOf(account));
/*     */     }
/*     */     
/* 215 */     if (allow) {
/* 216 */       ZimbraLog.account.debug("Retrieving group members from LDAP for group %s", new Object[] { group.getName() });
/* 217 */       return new GalGroupMembers.LdapDLMembers(group);
/*     */     }
/* 219 */     ZimbraLog.account.debug("account %s is not allowed to get members from ldap for group %s", new Object[] { account.getName(), group.getName() });
/*     */     
/* 221 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private int getOffset(Element request)
/*     */     throws ServiceException
/*     */   {
/* 252 */     int offset = 0;
/*     */     
/*     */ 
/* 255 */     String offsetStr = request.getAttribute("_offset", null);
/* 256 */     if (offsetStr != null) {
/* 257 */       offset = (int)Element.parseLong("_offset", offsetStr);
/*     */     }
/*     */     else {
/* 260 */       offsetStr = request.getAttribute("offset", null);
/* 261 */       if (offsetStr != null) {
/* 262 */         offset = (int)Element.parseLong("offset", offsetStr);
/*     */       }
/*     */     }
/*     */     
/* 266 */     if (offset < 0) {
/* 267 */       throw ServiceException.INVALID_REQUEST("offset" + offset + " is negative", null);
/*     */     }
/*     */     
/* 270 */     return offset;
/*     */   }
/*     */   
/*     */   private int getLimit(Element request) throws ServiceException
/*     */   {
/* 275 */     int limit = 0;
/*     */     
/*     */ 
/* 278 */     String limitStr = request.getAttribute("_limit", null);
/* 279 */     if (limitStr != null) {
/* 280 */       limit = (int)Element.parseLong("_limit", limitStr);
/*     */     }
/*     */     else {
/* 283 */       limitStr = request.getAttribute("limit", null);
/* 284 */       if (limitStr != null) {
/* 285 */         limit = (int)Element.parseLong("limit", limitStr);
/*     */       }
/*     */     }
/*     */     
/* 289 */     if (limit < 0) {
/* 290 */       throw ServiceException.INVALID_REQUEST("limit" + limit + " is negative", null);
/*     */     }
/*     */     
/* 293 */     return limit;
/*     */   }
/*     */   
/*     */ 
/*     */   protected Element processDLMembers(ZimbraSoapContext zsc, String dlName, Account account, int limit, int offset, GalGroupMembers.DLMembers dlMembers)
/*     */     throws ServiceException
/*     */   {
/* 300 */     if (!GalSearchControl.canExpandGalGroup(dlName, dlMembers.getDLZimbraId(), account)) {
/* 301 */       ZimbraLog.misc.warn("dlName: %s, dlMembers(%s) size %d, id %s", new Object[] { dlName, dlMembers.getClass(), Integer.valueOf(dlMembers.getTotal()), dlMembers.getDLZimbraId() });
/* 302 */       Group dl = Provisioning.getInstance().getGroup(Key.DistributionListBy.name, dlName);
/* 303 */       if (dl == null) {
/* 304 */         throw ServiceException.NOT_FOUND(dlName + " does not exist");
/*     */       }
/* 306 */       throw ServiceException.PERM_DENIED("can not access dl members: " + dlName);
/*     */     }
/*     */     
/*     */ 
/* 310 */     Element response = zsc.createElement(AccountConstants.GET_DISTRIBUTION_LIST_MEMBERS_RESPONSE);
/* 311 */     if (dlMembers != null) {
/* 312 */       int numMembers = dlMembers.getTotal();
/*     */       
/* 314 */       if ((offset > 0) && (offset >= numMembers)) {
/* 315 */         throw ServiceException.INVALID_REQUEST("offset " + offset + " greater than size " + numMembers, null);
/*     */       }
/*     */       
/*     */ 
/* 319 */       int endIndex = offset + limit;
/* 320 */       if (limit == 0) {
/* 321 */         endIndex = numMembers;
/*     */       }
/* 323 */       if (endIndex > numMembers) {
/* 324 */         endIndex = numMembers;
/*     */       }
/*     */       
/* 327 */       dlMembers.encodeMembers(offset, endIndex, response);
/*     */       
/* 329 */       response.addAttribute("more", endIndex < numMembers);
/* 330 */       response.addAttribute("total", numMembers);
/*     */     }
/*     */     
/* 333 */     return response;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/account/GetDistributionListMembers.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */